<?php

class newHotelSearch {

    //start page sort - without providers
    public function checkSort($sort, $page) {
        $hotels = null;
        $offset = ($page * sbValues::HOTELS_IN_PAGE) - sbValues::HOTELS_IN_PAGE;

        switch ($sort) {
            case sbValues::SORT_BY_PRICE:
                $prices = $this->sortByPrice($offset);
                $hotels = $this->findHotelsByPrice($prices);
                break;
            case sbValues::SORT_BY_NAME:
                $data = $this->sortHotelsByName($offset);
                $hotels = $this->insertRooms($data);
                break;
            case sbValues::SORT_BY_RANK:
                $data = $this->sortHotelsByRank($offset);
                $hotels = $this->insertRooms($data);
                break;
        }

        return $hotels;
    }

    public function allAvailableHotels() {
        $count = Doctrine_Core::getTable('tblHotel')->createQuery('e')->count();
        return floor($count / sbValues::HOTELS_IN_PAGE);
    }

    public function allSorts() {
        return array(
            array('value' => 'price', 'name' => 'Price'),
            array('value' => 'rank', 'name' => 'Hotel rank'),
            array('value' => 'name', 'name' => 'Hotel name')
        );
    }

    /*
     * Sort local by price
     */

    public function sortByPrice($offset) {
        return Doctrine_Query::create()->
                        select('*')->
                        from('tblPrice')->
                        where('bar_price_usd>? and date=?', array(0, sbValues::getDateToDB()))->
                        groupBy('hotel_id')->
                        orderBy('bar_price_usd ASC')->
                        offset($offset)->
                        limit(sbValues::HOTELS_IN_PAGE)->execute();
    }

    public function findHotelsByPrice($prices) {
        $hotels = array();
        foreach ($prices as $p) {
            $h = Doctrine_Query::create()->select('r.*, c.name_en, g.name_en, s.name_en')->from('tblHotel r')->
                    innerJoin('r.CountryHotel c')->
                    innerJoin('r.RegionHotel g')->
                    innerJoin('r.CityHotel s')->
                    where('r.id=?', $p->getHotelId())->
                    execute();

            $rooms = Doctrine_Query::create()->select('r.*, p.*')->from('tblRooms r')->
                    innerJoin('r.RoomPrice p')->
                    where('r.hotel_id=? and p.bar_price_usd>? and p.date=?', array($p->getHotelId(), 0, sbValues::getDateToDB()))->
                    execute();
            $harr = $h->toArray();
            $harr[0]['Rooms'] = $rooms->toArray();
            array_push($hotels, $harr[0]);
        }
        return $hotels;
    }

    /*
     * Sort local by name
     */

    public function sortHotelsByName($offset) {
        return Doctrine_Query::create()->
                        select('r.*, c.name_en, g.name_en, s.name_en')->
                        from('tblHotel r')->
                        innerJoin('r.CountryHotel c')->
                        innerJoin('r.RegionHotel g')->
                        innerJoin('r.CityHotel s')->
                        orderBy('r.name_en ASC')->
                        offset($offset)->
                        limit(sbValues::HOTELS_IN_PAGE)->
                        execute();
    }

    /*
     * Sort local by rank
     */

    public function sortHotelsByRank($offset) {
        return Doctrine_Query::create()->
                        select('r.*, c.name_en, g.name_en, s.name_en')->
                        from('tblHotel r')->
                        innerJoin('r.CountryHotel c')->
                        innerJoin('r.RegionHotel g')->
                        innerJoin('r.CityHotel s')->
                        orderBy('stars DESC')->
                        offset($offset)->
                        limit(sbValues::HOTELS_IN_PAGE)->
                        execute();
    }

    public function insertRooms($hotels) {
        $fullHotels = array();
        foreach ($hotels as $h) {
            $rooms = Doctrine_Query::create()->
                    select('r.*, p.*')->
                    from('tblRooms r')->
                    innerJoin('r.RoomPrice p')->
                    where('r.hotel_id=? and p.date=?', array($h->getId(), sbValues::getDateToDB()))->
                    execute();
            $harr = $h->toArray();
            if ($rooms) {
                $harr['Rooms'] = $rooms->toArray();
            } else {
                $harr['Rooms'] = 'Not available rooms';
            }
            array_push($fullHotels, $harr);
        }

        return $fullHotels;
    }

    public function searchHotels($city_id, $hotel_id, $date, $days, $rooms, $stars, $type) {
        $parseStars = $this->parseStars($stars);
        $parseRooms = $this->parseRooms($rooms);

        if ($hotel_id == 0) {
            $data = $this->searchHotelsOnline($city_id, $parseStars);
            $hotels = $this->checkAvailibleRoom($data, $date, $days, $parseRooms, $type);
        } else {
            $data = $this->searchHotelByName($hotel_id);
            $hotels = $this->checkAvailibleRoom($data, $date, $days, $parseRooms, $type);
        }

        return $hotels;
    }

    public function afterSearchPages($hotels) {
        return floor($hotels / sbValues::HOTELS_IN_PAGE) + 1;
    }

    public function returnsHotelsToPage($page, $hotels) {
        $offset = ($page * sbValues::HOTELS_IN_PAGE) - sbValues::HOTELS_IN_PAGE;
        $arr = array();
        for ($i = $offset; $i <= ($page * sbValues::HOTELS_IN_PAGE) && $i < count($hotels); $i++) {
            array_push($arr, $hotels[$i]);
        }

        return $arr;
    }

    public function searchHotelsOnline($city_id, $stars) {
        $hotels = Doctrine_Query::create()->
                select('r.*, c.name_en, g.name_en, s.name_en')->
                from('tblHotel r')->
                innerJoin('r.CountryHotel c')->
                innerJoin('r.RegionHotel g')->
                innerJoin('r.CityHotel s')->
                where('r.city_id=? AND r.stars BETWEEN ? AND ?', array($city_id, $stars['min'], $stars['max']))->
                orderBy('r.stars DESC')->
                execute();

        return $hotels;
    }

    public function searchHotelByName($hotel_id) {
        return Doctrine_Query::create()->
                        select('r.*, c.name_en, g.name_en, s.name_en')->
                        from('tblHotel r')->
                        leftJoin('r.CountryHotel c')->
                        leftJoin('r.RegionHotel g')->
                        leftJoin('r.CityHotel s')->
                        where('r.id=?', $hotel_id)->
                        execute();
    }

    public function checkAvailibleRoom($hotels, $date, $days, $rooms, $type) {
        $arrayHotels = array();
        $goc = new sbGoc();
        $connectGoc = $goc->connect();
        $isratel = new sbIsratel();
        $connectIsratel = $isratel->connect($type);
        foreach ($hotels as $hotel) {
            switch ($hotel->getProviderId()) {
                case sbValues::PROVIDER_GOC:
                    if ($connectGoc['status']) {
                        $search = $goc->search($connectGoc['cause'], $hotel->getProviderCode(), $date, $days, $rooms);
                        $prices = $goc->parseSearchResult($search);
                        if ($prices) {
                            $arrRooms = $this->insertHotelRooms($hotel->getId(), $prices);
                            $hotel = $hotel->toArray();
                            $hotel['Rooms'] = $arrRooms;
                            array_push($arrayHotels, $hotel);
                        }
                    }
                    break;
                case sbValues::PROVIDER_ISR:
                    $search = $isratel->search($connectIsratel, $hotel->getProviderCode(), $date, $days, $rooms);
                    $prices = $isratel->parseSearchResult($search);
                    if ($prices) {
                        $arrRooms = $this->insertHotelRooms($hotel->getId(), $prices);
                        $hotel = $hotel->toArray();
                        $hotel['Rooms'] = $arrRooms;
                        array_push($arrayHotels, $hotel);
                    }
                    break;
                case sbValues::PROVIDER_DAN:

                    break;
            }
        }

        return $arrayHotels;
    }

    public function insertHotelRooms($hotel_id, $prices) {
        $rooms = array();
        if ($prices) {
            foreach ($prices as $p) {
                $room = Doctrine_Query::create()->
                        select('*')->
                        from('tblRooms')->
                        where('hotel_id=? and code_name=?', array($hotel_id, $p['room_code']))->
                        execute();

                $roomArr = $room->toArray();
                if ($roomArr) {
                    $roomArr[0]['RoomPrice'] = $p;
                    array_push($rooms, $roomArr[0]);
                }
            }
            return $rooms;
        } else {
            return null;
        }
    }

    public function parseRooms($rooms) {
        $parse = explode(';', $rooms);
        $arrayRooms = array();
        for ($i = 0; $i < count($parse) - 1; $i++) {
            $room = explode(':', $parse[$i]);
            $temp = array(
                'room' => $room[0],
                'adult' => $room[1],
                'children' => $room[2],
                'baby' => $room[3]
            );

            array_push($arrayRooms, $temp);
        }

        return $arrayRooms;
    }

    public function parseStars($stars) {
        $parse = explode(',', $stars);
        return array(
            'min' => $parse[0],
            'max' => $parse[1]
        );
    }

}
